package com.ruoyi.web.controller.laiqianba.merchantdata;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.account.service.IAccountMerchantInfoHisService;
import com.ruoyi.account.service.IAccountMerchantInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.db.domain.AccountMerchantInfo;
import com.ruoyi.db.domain.AccountMerchantInfoHis;
import com.ruoyi.db.domain.MerchantBaseInfo;
import com.ruoyi.merchant.service.IMerchantBaseInfoService;
import com.ruoyi.web.wrapper.AccountMerchantInfoHisWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 当前商户账单
 *
 * @author ruoyi
 * @date 2023-09-02
 */
@RestController
@RequestMapping("/merchantdata/merchantInfo")
public class MerchantDataInfoController extends BaseController {

    @Autowired
    private IAccountMerchantInfoService accountMerchantInfoService;
    @Autowired
    IMerchantBaseInfoService merchantBaseInfoService;

    /**
     * 查询商户账户信息列表
     */
    @GetMapping("/merchantInfoData")
    public AjaxResult merchantInfoData() {

        LoginUser loginUser = getLoginUser();
        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoService.selectMerchantBaseInfoByDeptId(loginUser.getDeptId());
        JSONObject accountData = new JSONObject();
        if (ObjectUtil.isNotEmpty(merchantBaseInfo)) {
            AccountMerchantInfo accountMerchantInfo = accountMerchantInfoService.selectAccountMerchantInfoByInfoId(merchantBaseInfo.getId());
            if (ObjectUtil.isNotEmpty(accountMerchantInfo)) {
                accountData.put("totalAmount", accountMerchantInfo.getTotalAmount());
            }
            JSONObject infoData = new JSONObject();
            infoData.put("infoName", merchantBaseInfo.getInfoName());
            infoData.put("companyName", merchantBaseInfo.getCompanyName());

            JSONObject returnObject = new JSONObject();
            returnObject.put("accountData", accountData);
            returnObject.put("infoData", infoData);
            return AjaxResult.success(returnObject);
        }

        return AjaxResult.success();
    }

    @Autowired
    IAccountMerchantInfoHisService accountMerchantInfoHisService;

    /**
     * 查询商户账户信息列表
     * 账户历史
     */
    @PreAuthorize("@ss.hasPermi('merchantdata:merchantInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccountMerchantInfoHis accountMerchantInfoHis) {
        startPage();
        List<Map<String, Object>> list = accountMerchantInfoHisService.selectAccountMerchantInfoHisMapList(accountMerchantInfoHis);
        list = new AccountMerchantInfoHisWrapper(list).wrap();
        return getDataTable(list);
    }


}
